package com.iotp.service.sys;


import com.iotp.dao.organization.OrganizationDAO;
import com.iotp.dao.user.CommUserMapper;
import com.iotp.dao.userUserRef.UserUserRefDAO;
import com.iotp.entity.base.CodeEnum;
import com.iotp.entity.base.DataMessage;
import com.iotp.entity.organization.Organization;
import com.iotp.entity.user.CommUser;
import com.iotp.entity.userUserRef.UserUserRef;
import com.iotp.service.base.BaseService;
import com.iotp.utils.common.DataUtil;
import com.iotp.utils.exception.NoFoundExcepiton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service("sysService")
public class SysServiceImpl extends BaseService implements SysService {
    @Autowired
    private CommUserMapper commUserMapper;//用户
    @Autowired
    private UserUserRefDAO userUserRefDAO;
    @Autowired
    private OrganizationDAO organizationDAO;


    /**
     * 登录
     * @param userName
     * @param password
     * @return
     */
    @Override
    public DataMessage Login(String userName, String password) {
        DataMessage dm = new DataMessage();
        if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)){
            dm.setResult(CodeEnum.参数不合法.code);
            dm.setMessage(CodeEnum.参数不合法.msg);
            dm.setData(null);
        }else{
            CommUser commUser = new CommUser();
            commUser.setItCode(userName);
            commUser.setUserPassword(DataUtil.generateMD5(password.getBytes()));
            List<CommUser> userList = this.commUserMapper.selectByCondition(commUser);
            if(userList != null && !userList.isEmpty()){
                CommUser cur_user = userList.get(0);
                if(cur_user.getAvailable() == 1){
                    dm.setResult(CodeEnum.成功.code);
                    dm.setMessage(CodeEnum.成功.msg);
                    Map<String, Object> map = new HashMap<>();
                    UserUserRef query = new UserUserRef();
                    query.setOldUserId(cur_user.getUserId());
                    List<UserUserRef> userUserRefs = this.userUserRefDAO.selectByCondition(query);
                    if(null != userUserRefs && !userUserRefs.isEmpty()){
                        cur_user.setNewuserId(userUserRefs.get(0).getId());
                    }else{
                        try {
                            throw new NoFoundExcepiton("新旧用户无关联");
                        } catch (NoFoundExcepiton noFoundExcepiton) {
                            noFoundExcepiton.printStackTrace();
                        }
                    }
                    map.put("cur_user", cur_user);
                    List<Organization> orgList = this.organizationDAO.selectUserOrgs(cur_user.getNewuserId());
                    if(null != orgList && !orgList.isEmpty()){
                        Organization org = orgList.get(0);
                        map.put("cur_org", org);
                    }else{
                        try {
                            throw new NoFoundExcepiton("用户机构无关联");
                        } catch (NoFoundExcepiton noFoundExcepiton) {
                            noFoundExcepiton.printStackTrace();
                        }
                    }
                    dm.setData(map);
                }else{
                    dm.setResult(CodeEnum.该用户被删除.code);
                    dm.setMessage(CodeEnum.该用户被删除.msg);
                    dm.setData(null);
                }
            }else{
                dm.setResult(1);
                dm.setMessage("账号或者密码错误");
                dm.setData(null);
            }
        }
        return dm;
    }





}
